#!/usr/bin/env ruby
puts "going and getting..."

# require AR
require 'rubygems'
require 'active_record'


meph_connect = {
  :adapter => "mysql",
  :database => "mephisto_development",
  :username => "root",
  :password => "killjoy",
  :host => "localhost"
}

uniblog_connect = {
  :adapter => "mysql",
  :database => "uniblog_development",
  :username => "root",
  :password => "killjoy",
  :host => "localhost"
}

class Content < ActiveRecord::Base 
end

Content.establish_connection(meph_connect)

class Article < Content 
end

class Entry < ActiveRecord::Base 
end

Entry.establish_connection(uniblog_connect)

# Article.find(:all, :conditions => { :article_id => nil }).each do |c|
#   Entry.create(:user_id => c.user_id, :title => c.title, :permalink => c.permalink, :summary => c.excerpt, :body => c.body, :created_at => c.created_at, :updated_at => c.updated_at, :published_at => c.published_at, :author_name => c.author, :original_id => c.id, :active => true, :version => 1)
# end

class Section < ActiveRecord::Base 
end
  
class AssignedSection < ActiveRecord::Base 
  has_one :article
  has_one :section 
end

Section.establish_connection(meph_connect)
AssignedSection.establish_connection(meph_connect)

class Category < ActiveRecord::Base 
end

class Categorization < ActiveRecord::Base 
end

Category.establish_connection(uniblog_connect)
Categorization.establish_connection(uniblog_connect)

# Section.find(:all, :order => "position").each do |s|
#   cat = Category.create(:name => s.name, :active => true, :original_id => s.id)
#   AssignedSection.find(:all, :conditions => { :section_id => s.id }).each do |as|
#     ent = Entry.find_by_original_id(as.article_id)
#     Categorization.create(:category_id => cat.id, :entry_id => ent.id)
#   end
# end

class Comment < ActiveRecord::Base 
end

Comment.establish_connection(uniblog_connect)

Content.find(:all, :conditions => "article_id > 0").each do |a|
    puts a.id
    ent = Entry.find_by_original_id(a.article_id)
    Comment.create(:entry_id => ent.id, :title => a.title, :name => a.author, :email => a.author_email, :url => a.author_url, :created_at => a.created_at, :updated_at => a.updated_at, :approved_at => a.published_at, :content => a.body) if ent && a.approved
end

puts "and gone."